<!DOCTYPE html>
<!--
Copyright (C) 2017 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-admin-view</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-admin-view.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <gr-admin-view></gr-admin-view>
  </template>
</test-fixture>

<script>
  suite('gr-admin-view tests', () => {
    let element;
    let sandbox;

    setup(() => {
      sandbox = sinon.sandbox.create();
      element = fixture('basic');
      stub('gr-rest-api-interface', {
        getProjectConfig() {
          return Promise.resolve({});
        },
      });
    });

    teardown(() => {
      sandbox.restore();
    });

    test('_computeURLHelper', () => {
      const path = '/test';
      const host = 'http://www.testsite.com';
      const computedPath = element._computeURLHelper(host, path);
      assert.equal(computedPath, '//http://www.testsite.com/test');
    });

    test('link URLs', () => {
      assert.equal(
          element._computeLinkURL({url: '/test'}),
          '//' + window.location.host + '/test');
      assert.equal(
          element._computeLinkURL({url: '/test', target: '_blank'}),
          '/test');
    });

    test('current page gets selected and is displayed', () => {
      element._filteredLinks = [{
        name: 'Projects',
        url: '/admin/projects',
        view: 'gr-admin-project-list',
        children: [],
      }];

      element.params = {
        adminView: 'gr-admin-project-list',
      };

      flushAsynchronousOperations();
      assert.equal(Polymer.dom(element.root).querySelectorAll(
          '.selected').length, 1);
      assert.ok(element.$$('gr-admin-project-list'));
      assert.isNotOk(element.$$('gr-admin-create-project'));
    });

    test('_filteredLinks admin', done => {
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
        return Promise.resolve({
          createGroup: true,
          createProject: true,
          viewPlugins: true,
        });
      });
      element._loadAccountCapabilities().then(() => {
        assert.equal(element._filteredLinks.length, 3);

        // Projects
        assert.equal(element._filteredLinks[0].children.length, 0);
        assert.isNotOk(element._filteredLinks[0].subsection);

        // Groups
        assert.equal(element._filteredLinks[1].children.length, 0);

        // Plugins
        assert.equal(element._filteredLinks[2].children.length, 0);
        done();
      });
    });

    test('_filteredLinks non admin authenticated', done => {
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
        return Promise.resolve({});
      });
      element._loadAccountCapabilities().then(() => {
        assert.equal(element._filteredLinks.length, 2);

        // Projects
        assert.equal(element._filteredLinks[0].children.length, 0);
        assert.isNotOk(element._filteredLinks[0].subsection);

        // Groups
        assert.equal(element._filteredLinks[1].children.length, 0);
        done();
      });
    });

    test('_filteredLinks non admin unathenticated', done => {
      element.reload().then(() => {
        assert.equal(element._filteredLinks.length, 1);

        // Projects
        assert.equal(element._filteredLinks[0].children.length, 0);
        assert.isNotOk(element._filteredLinks[0].subsection);
        done();
      });
    });

    test('Project shows up in nav', done => {
      element._projectName = 'Test Project';
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
        return Promise.resolve({
          createGroup: true,
          createProject: true,
          viewPlugins: true,
        });
      });
      element._loadAccountCapabilities().then(() => {
        assert.equal(element._filteredLinks.length, 3);

        // Projects
        assert.equal(element._filteredLinks[0].children.length, 0);
        assert.equal(element._filteredLinks[0].subsection.name, 'Test Project');

        // Groups
        assert.equal(element._filteredLinks[1].children.length, 0);

        // Plugins
        assert.equal(element._filteredLinks[2].children.length, 0);
        done();
      });
    });

    test('Nav is reloaded when project changes', () => {
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
        return Promise.resolve({
          createGroup: true,
          createProject: true,
          viewPlugins: true,
        });
      });
      sandbox.stub(element.$.restAPI, 'getAccount', () => {
        return Promise.resolve({_id: 1});
      });
      sandbox.stub(element, 'reload');
      element.params = {project: 'Test Project', adminView: 'gr-project'};
      assert.equal(element.reload.callCount, 1);
      element.params = {project: 'Test Project 2',
        adminView: 'gr-project'};
      assert.equal(element.reload.callCount, 2);
    });

    test('Nav is reloaded when group changes', () => {
      sandbox.stub(element, '_computeGroupName');
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
        return Promise.resolve({
          createGroup: true,
          createProject: true,
          viewPlugins: true,
        });
      });
      sandbox.stub(element.$.restAPI, 'getAccount', () => {
        return Promise.resolve({_id: 1});
      });
      sandbox.stub(element, 'reload');
      element.params = {groupId: '1', adminView: 'gr-group'};
      assert.equal(element.reload.callCount, 1);
    });

    test('Nav is reloaded when group name changes', done => {
      const newName = 'newName';
      sandbox.stub(element, '_computeGroupName');
      sandbox.stub(element, 'reload', () => {
        assert.equal(element._groupName, newName);
        assert.isTrue(element.reload.called);
        done();
      });
      element.params = {group: 1, adminView: 'gr-group'};
      element._groupName = 'oldName';
      flushAsynchronousOperations();
      element.$$('gr-group').fire('name-changed', {name: newName});
    });
  });
</script>
